import { createRouter, createWebHashHistory } from 'vue-router'

import { getCookie } from '@/utils/cookie'

import { createPinia } from "pinia"
import { useCounterStore } from '@/stores/counter'
const pinia = createPinia()
const store = useCounterStore(pinia)
import { storeToRefs } from 'pinia'
const { changeLoginStatus } = store
const { isLogin } = storeToRefs(store)

import AdminHome from '@/views/AdminHome.vue'
import AdminLogin from '@/views/AdminLogin.vue'
import NotFound from '@/components/NotFound.vue'

import AuthAdd from '@/views/auth/AuthAdd.vue'
import AuthList from '@/views/auth/AuthList.vue'

import UserAdd from '@/views/user/UserAdd.vue'
import UserList from '@/views/user/UserList.vue'

import RoleList from '@/views/role/RoleList.vue'
import RoleAdd from '@/views/role/RoleAdd.vue'
import RoleEdit from '@/views/role/RoleEdit.vue'

import CatList from '@/views/cat/CatList.vue'

import DishList from '@/views/dish/DishList.vue'
import DishAdd from '@/views/dish/DishAdd.vue'
import DishEdit from '@/views/dish/DishEdit.vue'



const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/home',
      component: AdminHome,
      children: [
        {
          path: '/auth/add',
          component: AuthAdd
        },
        {
          path: '/auth/list',
          component: AuthList
        },
        {
          path: '/user/add',
          component: UserAdd
        },
        {
          path: '/user/list',
          component: UserList
        },
        {
          path: '/role/list',
          component: RoleList
        },
        {
          path: '/role/add',
          component: RoleAdd
        },
        {
          path: '/role/edit/:id',
          component: RoleEdit
        },
        {
          path: '/cat/list',
          component: CatList
        },
        {
          path: '/dish/list',
          component: DishList
        },
        // {
        //   path: '/dish/add',
        //   component: DishAdd
        // },
        // {
        //   path: '/dish/edit',
        //   component: DishEdit
        // }
      ]
    },
    {
      path: '/login',
      component: AdminLogin,
    },
    {
      path: '/404',
      component: NotFound
    },
    // {
    //   path: '*',
    //   redirect: '/404'
    // }
  ]
})

// // 路由守卫 - 判断用户是否登录
// router.beforeEach((to, from, next) => {
//   if (to.path === '/login' || to.path === '/404') {
//     // 放行
//     next()
//     return
//   }
//   // 验证cookie
//   let adminName = getCookie('adminName')
//   if (adminName) {
//     // 设置状态
//     changeLoginStatus()
//     // 验证vuex是否是登录状态
//     if (isLogin) {
//       // 如果是去首页，放行
//       if (to.path === '/home') {
//         next()
//         return
//       }
//       // 验证权限
//       // 将当前用户的权限获取
//       let auths = sessionStorage.getItem('auths')
//       // 将所有子权限路径组成一个数组
//       auths = JSON.parse(auths)
//       // 遍历
//       let arr = []
//       auths.forEach(item => {
//         item.children && item.children.length && arr.push(...(item.children.map(v => v.path)))
//       })
//       // console.log(arr);
//       // 判断to.pathshifou在数组中
//       // 数to.path中有几个斜杠
//       let count = 0
//       let path = to.path
//       for (let a = 0; a < to.path.length; a++) {
//         if (to.path[a] === '/') {
//           count++
//           if (count > 2) {
//             path = to.path.slice(0, a)
//             break
//           }
//         }
//       }
//       // 判断path是否在数组中
//       if (arr.includes(path)) {
//         next()
//       } else {
//         router.push('/404')
//         return
//       }
//       next()
//       return
//     } else {
//       // 拦截
//       router.push('/login')
//       return
//     }
//   } else {
//     // 拦截
//     router.push('/login')
//     return
//   }
// })

export default router
